<?php
namespace app\admin\controller;

use think\Db;

class Publics extends Base
{
    /**
     * 退出
     */
    public function logOut(){
        $adminUser = new \app\admin\model\AdminUser();
        $adminUser->logOut();
        r(url('Publics/logIn'));
    }
    
    /**
     * 登陆
     */
    public function logIn(){
        $adminUser = new \app\admin\model\AdminUser();
        if( $adminUser->isLogin() ){
            r(url('Index/index'));
        }
        if( is_post() ){
            $username       = input('username');
            $password       = input('password');
            $code           = input('code');
            $is_remember    = input('is_remember') ? true : false;
            
            //验证
            $rule = [
                   'username'   =>  'require',
                   'password'   =>  'require',
                   'code'       =>  'require',
                ];
            $msg = [
                   'username.require'   =>  '用户名必填',
                   'password.require'   =>  '密码必填',
                   'code.require'       =>  '验证码必填',
                ];
            $data =  [
                   'username'   =>  $username,
                   'password'   =>  $password,
                   'code'       =>  $code,
                ];
            com_validate( $rule, $msg, $data, true );
            
            //验证码
            $v = new \org\Verify();
            if( !$v->check($code) ){
                com_error(['code'=>'验证码错误!']);
                com_check();
            }
            
            //登录
            $adminUser->logIn($username, $password, $is_remember);
            $config = [
                'suc'=>[
                    'url'=>url('Index/index'),
                ]
            ];
            com_check( $config );
        }

        $this->view->engine->layout(false);
        return $this->fetch();
    }
    


    /**
     * 验证码
     */
    public function verify(){
        $config = [
            'codeSet'  => '123456789',
            'expire'   => 1800,
            'fontSize' => 18, // 验证码字体大小(px)
            'useCurve' => false, // 是否画混淆曲线
            'useNoise' => false, // 是否添加杂点
            'imageH'   => 40, // 验证码图片高度
            'imageW'   => 140, // 验证码图片宽度
            'length'   => 4, // 验证码位数
            'fontttf'  => '', // 验证码字体，不设置随机获取
            'bg'       => [243, 251, 254], // 背景颜色
            'reset'    => true, // 验证成功后是否重置
        ];
        $verify = new \org\Verify( $config );
        return $verify->entry();
    }
    
    //结算
    public function countSys( $type='hand' ){
        set_time_limit(0);
        ini_set('memory_limit', '500M');
        
        $sys = new \app\common\model\Sys();
        $c   = new \app\common\server\CountAward();
        $s   = new \app\common\server\SendAward();
        
        if($type=='hand'){
            if(!session("?admin_user_id"))
                exit('非法操作....');
        }else{
            $day = $sys->get('count_day');
            switch ($day){
                case 1:
                  break;  
                case 2:

                case 3:
                
                default:
                    exit('非法操作___');
            }       
            
        }
        
        e('开始结算,关闭系统');
        $sys->set('is_close_all',1);
        $this->delBd();
        $c->count();
        $s->sendDb();
        $s->send();
        $sys->set('is_close_all',0);
        e('打开系统');
        com_check();
    }
    
    //删除24小时还未激活的报单
    public function delbd(){
        $time = time() - 24*60*60;
        $d   = new \app\common\server\DelBaodan();
        $rs = Db::name('order_baodan')->field('id')->where('status',0)->where('create_time','<', $time)->select();
        if($rs){
            foreach($rs as $v){
                $d->doit($v['id'],'sys');
            }
        }
    }
}
